#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Time    :   2021/08/12 20:00:18
@Author  :   Leo Wood 
@Contact :   leowood@foxmail.com
'''


import json
from pyMySql import pySql
import os
import sys
os.chdir(sys.path[0])
from tqdm import tqdm

with open('db.json', 'r', encoding='utf-8') as f:
    db_info = json.load(f)
db_info = db_info['Medical_Knowledge']

label2id = {}

term_id = 1000


def mesh_txt_to_sql():

    db_server = pySql(ip=db_info['ip'], user=db_info['user'], pwd=db_info['pwd'], db=db_info['db'])
    with open('/Users/leo/OneDrive/Phd/实验数据/医学知识库收集/CMeSH.txt','r') as f:
        for line in tqdm(f.readlines()):
            line = line.strip()
            if line:
                term_id += 1
                term_label = ""
                term_text = ""
                term_text_original = ""
                super_term_id = 0
                term_level = 0

                line = line.strip()
                term_label = line.split(' ')[0]
                

                if ", " in line:
                    term_text_original = line.replace(term_label,"").strip()
                    term_text = "".join(reversed(term_text_original.split(", ")))
                else:
                    if len(line.split(' ')) == 2:
                        term_text = line.split(' ')[1]
                    else:
                        term_text = " ".join(line.split(' ')[1:])
                    
    
                label2id[term_label] = term_id

                if "." not in term_label:
                    if len(term_label) > 1:
                        super_label = term_label[0]
                        super_term_id = label2id[super_label]
                        term_level = 2
                    else:
                        term_level = 1
                else:
                    super_label = ".".join(term_label.split(".")[:-1])
                    super_term_id = label2id[super_label]
                    term_level = len(term_label.split(".")) + 1


                term_text = term_text.replace("'","''") ## 转义字符
                
                sql = "insert into Terms(termId,termLabel,termText,termOriginalText,superTermId,termLevel) values ({},'{}','{}','{}',{},{})".format(term_id,term_label,term_text,term_text_original,super_term_id,term_level)
                db_server.write_sql(sql)
    db_server.close()

def all_chinese(text):
    if text:
        for char in text:
            if not (char >= '\u4e00' and char <= '\u9fa5'):
                return False
        return True
    return False

def read_mesh_db():
    db_server = pySql(ip=db_info['ip'], user=db_info['user'], pwd=db_info['pwd'], db=db_info['db'])

    sql = "select termText from Terms"

    resuts = db_server.read_sql(sql)

    mesh_word_list = [result[0] for result in resuts]

    with open('mesh_word_list_all_cn.txt','w') as f:
        for word in mesh_word_list:
            if all_chinese(word):
                f.write(word + '\n')

    db_server.close()
                    

if __name__ == '__main__':
    read_mesh_db()